home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / conv / Acvt.lha / Acvt 1.07 / sources / adsk_scp.cpp < prev    next >
C/C++ Source or Header  |  2001-03-08  |  5KB  |  282 lines

  1. //    This program is free software; you can redistribute it and/or modify
  2. //    it under the terms of the GNU General Public License as published by
  3. //    the Free Software Foundation; either version 2 of the License, or
  4. //    any later version.
  5. //
  6. //    This program is distributed in the hope that it will be useful,
  7. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. //    GNU General Public License for more details.
  10. //
  11. //    You should have received a copy of the GNU General Public License
  12. //    along with this program; if not, write to the Free Software
  13. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. //
  15.  
  16. #include "adsk_scp.h"
  17. #include "autil.h"
  18. #include "cfile.h"
  19.  
  20. #define SCP_MAGIC 0xFDFD
  21.  
  22. CScp::CScp() : ADisk()
  23. {
  24.     #ifdef _MEMORY_DUMP_
  25.         printf( "CScp constructed: %p\n", this );
  26.     #endif
  27. }
  28.  
  29. CScp::~CScp()
  30. {
  31.     #ifdef _MEMORY_DUMP_
  32.         printf( "CScp destructed: %p\n", this );
  33.     #endif
  34. }
  35.  
  36. #ifndef __CDISK_NOLOAD__
  37. BOOL CScp::Load( char* szFname, BOOL, BOOL )
  38. {
  39.     WORD    wMagic;
  40.     BYTE    btSectorSize;
  41.     BYTE    btTracks;
  42.     BYTE    btSectorsPerTrack;
  43.  
  44.     CFile cf;
  45.  
  46.     if( !cf.Open( szFname ) )
  47.     {
  48.         sprintf( m_szLastError, "SCP: Can't open '%s'", szFname );
  49.         return FALSE;
  50.     }
  51.  
  52.     strcpy( m_szFname, szFname );
  53.  
  54.     wMagic = cf.readLEw();
  55.  
  56.     if ( wMagic != SCP_MAGIC )
  57.     {
  58.         sprintf( m_szLastError, "SCP: File '%s' is not an SCP file!", szFname );
  59.         return FALSE;
  60.     }
  61.  
  62.     btSectorSize = cf.readb();
  63.     btTracks = cf.readb();
  64.     btSectorsPerTrack = cf.readb();
  65.  
  66.     int iSectorSize;
  67.  
  68.     switch( btSectorSize )
  69.     {
  70.         case 0x80:
  71.             iSectorSize = 0x80;
  72.             break;
  73.  
  74.         case 0x00:
  75.             iSectorSize = 0x100;
  76.             break;
  77.  
  78.         default:
  79.         {
  80.             sprintf( m_szLastError, "SCP: Invalid sector size: %02X", btSectorSize );
  81.             return FALSE;
  82.         }
  83.     }
  84.  
  85.     DISK_GEOMETRY dg;
  86.     dg.iSides = 1;
  87.     dg.iTracks = btTracks;
  88.     dg.iSectorsPerTrack = btSectorsPerTrack;
  89.     dg.iBytesPerSector = iSectorSize;
  90.  
  91.     if ( !Format( &dg ) )
  92.         return FALSE;
  93.  
  94.     BYTE *pbtTable = new BYTE [ btSectorsPerTrack * btTracks ];
  95.  
  96.     if ( ! pbtTable )
  97.     {
  98.          sprintf( m_szLastError, "SCP: Not enough memory for sector table!" );
  99.          return FALSE;
  100.     }
  101.  
  102.     if ( !cf.Read( pbtTable, btSectorsPerTrack * btTracks ) )
  103.     {
  104.          sprintf( m_szLastError, "SCP: Can't read sector table!" );
  105.          return FALSE;
  106.     }
  107.  
  108.     BYTE abtBuff[ 0x100 ];
  109.     memset( abtBuff, 0, 0x100 );
  110.  
  111.     BYTE* pbtPtr = pbtTable;
  112.  
  113.     for( int iTrack = 0; iTrack < btTracks; iTrack++ )
  114.     {
  115.         for( int iSector = 0; iSector < btSectorsPerTrack; iSector++ )
  116.         {
  117.             if ( *pbtPtr )
  118.             {
  119.                 int iNowRead;
  120.  
  121.                 if ( !iTrack && ( iSector < 3 ) )
  122.                     iNowRead = 0x80;
  123.                 else
  124.                     iNowRead = iSectorSize;
  125.  
  126.                 int iReallyRead;
  127.  
  128.                 if ( !cf.Read( abtBuff, iNowRead, &iReallyRead ) || ( iNowRead != iReallyRead ) )
  129.                 {
  130.                     delete [] pbtTable;
  131.                      sprintf( m_szLastError, "SCP: Image broken!" );
  132.                      return FALSE;
  133.                 }
  134.  
  135.                 if ( !WriteSector( *pbtPtr + iTrack* btSectorsPerTrack, abtBuff ) )
  136.                 {
  137.                     delete [] pbtTable;
  138.                     return FALSE;
  139.                 }
  140.  
  141.             }
  142.             pbtPtr++;
  143.         }
  144.  
  145.     }
  146.  
  147.     delete [] pbtTable;
  148.  
  149.     cf.Close();
  150.     return TRUE;
  151.  
  152. }
  153. #endif
  154.  
  155. #ifdef __CDISK_SAVE__
  156.  
  157. BOOL CScp::Save( char* szOutFile, BOOL bOverWrite )
  158. {
  159.     if ( !bOverWrite && !access( szOutFile, F_OK ) )
  160.     {
  161.         sprintf( m_szLastError, "SCP: File already exists! '%s'", szOutFile );
  162.         return FALSE;
  163.     }
  164.  
  165.     BYTE btSize;
  166.     BYTE btTracks;
  167.     BYTE btSpT;
  168.  
  169.     switch ( m_geometry.iBytesPerSector )
  170.     {
  171.         case 0x80:
  172.             btSize = 0x80;
  173.             break;
  174.  
  175.         case 0x100:
  176.         default:
  177.             btSize = 0x00;
  178.             break;
  179.     }
  180.  
  181.     BOOL bGood = FALSE;
  182.  
  183.     btTracks = m_geometry.iTracks;
  184.     btSpT = m_geometry.iSectorsPerTrack;
  185.  
  186.     if ( ( m_geometry.iTracks == 40 ) && ( m_geometry.iSectorsPerTrack == 18 ) )
  187.         bGood = TRUE;
  188.  
  189.     if ( ( m_geometry.iTracks == 40 ) && ( m_geometry.iSectorsPerTrack == 26 ) )
  190.         bGood = TRUE;
  191.  
  192.     if ( !bGood )
  193.     {
  194.         sprintf( m_szLastError, "SCP: Can't export, because of invalid disk size!" );
  195.         return FALSE;
  196.     }
  197.  
  198.     int iMapSize = m_geometry.iTracks * m_geometry.iSectorsPerTrack;
  199.  
  200.     BYTE* pMap = new BYTE [ iMapSize ];
  201.  
  202.     if ( !pMap )
  203.     {
  204.         sprintf( m_szLastError, "SCP: Can't allocate memory for map!" );
  205.         return FALSE;
  206.     }
  207.  
  208.     memset( pMap, 0, iMapSize );
  209.  
  210.  
  211.     CFile cf;
  212.  
  213.     if ( !cf.Create( szOutFile ) )
  214.     {
  215.         sprintf( m_szLastError, "SCP: Can't create '%s'", szOutFile );
  216.         delete [] pMap;
  217.         return FALSE;
  218.     }
  219.  
  220.     WORD wMagic = SCP_MAGIC;
  221.  
  222.     cf.writeLEw( wMagic );
  223.     cf.writeb( btSize );
  224.     cf.writeb( btTracks );
  225.     cf.writeb( btSpT );
  226.  
  227.     cf.Seek( iMapSize, SEEK_CUR );
  228.  
  229.     BYTE abtBuff[ 0x100 ];
  230.  
  231.     int iSectors = m_geometry.iSectors;
  232.  
  233.     for( int i = 0; i < iSectors; i++ )
  234.     {
  235.         if ( !ReadSector( abtBuff, i + 1 ) )
  236.         {
  237.             delete [] pMap;
  238.             cf.Close();
  239.             unlink( szOutFile );
  240.             return FALSE;
  241.         }
  242.  
  243.         int iBytesNow = ( i < 3 ) ? 0x80 : m_geometry.iBytesPerSector;
  244.  
  245.         if ( !IsBlockEmpty( abtBuff, iBytesNow ) )
  246.         {
  247.             int iWritten;
  248.             if ( !cf.Write( abtBuff, iBytesNow, &iWritten ) || ( iBytesNow != iWritten ) )
  249.             {
  250.                 sprintf( m_szLastError, "SCP: Error writing to '%s'", szOutFile );
  251.                 delete [] pMap;
  252.                 cf.Close( );
  253.                 unlink( szOutFile );
  254.                 return FALSE;
  255.             
  256.             }
  257.  
  258.             pMap[ i ] = ( i % btSpT ) + 1;
  259.         }
  260.     }
  261.  
  262.     cf.Seek( 5, SEEK_SET );
  263.  
  264.     if ( !cf.Write( pMap, iMapSize ) )
  265.     {
  266.         sprintf( m_szLastError, "SCP: Can't write!" );
  267.         delete [] pMap;
  268.         cf.Close( );
  269.         unlink( szOutFile );
  270.         return FALSE;
  271.     
  272.     }
  273.  
  274.     delete [] pMap;
  275.  
  276.     cf.Close();
  277.  
  278.     return TRUE;
  279. }
  280.  
  281. #endif //__CDISK_SAVE__
  282.